import React from 'react';
import { observer, inject } from 'mobx-react';
import * as mobx from 'mobx';
import { WeaTop, WeaTab, WeaSelect, WeaTable, WeaFormItem, WeaInputSearch } from 'ecCom';
import { Button } from 'antd';

import * as _ from '../../util/common';
import { singleConvert, multiConvert, doDuplicate } from '../../util/analyzeUtil';
import WeaSelectTransfer from './wf-selectitem-browser/WeaSelectTranfer';

@inject('analyzeStore')
@observer
export default class linkageInfo extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        const { fieldOptions, linkage_typeValue, linkage_fieldTypeValue, linkage_fieldValue } = this.props.analyzeStore;
        const columns = [
            {
                title: '联动类型',
                dataIndex: 'linkageType',
                key: 'linkageType',
                width: "20%",
            }, {
                title: '触发字段',
                dataIndex: 'trgField',
                key: 'trgField',
                width: "20%",
            }, {
                title: '赋值字段',
                dataIndex: 'assignField',
                key: 'assignField',
                width: "20%",
            }, {
                title: '其他信息',
                dataIndex: 'otherInfo',
                key: 'otherInfo',
                width: "20%",
            }];

        return (
            <div className='wf-analyze-linkageInfo'>
                <div className='wf-linkageInfo-queryItems' >
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@eap97t`}
                        style={{ width: '30%' }}
                        label={'联动类型'}
                        labelCol={{ span: 5 }}
                        wrapperCol={{ span: 12 }}
                    >
                        <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@n4ndpo`}
                            options={[
                                { "key": "0", "showname": "全部", },
                                { "key": "1", "showname": "行规则" },
                                { "key": "2", "showname": "列赋值" },
                                { "key": "3", "showname": "SQL联动" },
                                { "key": "4", "showname": "时间日期计算" },
                                { "key": "5", "showname": "字段赋值" },
                                { "key": "6", "showname": "选择框联动" },
                                { "key": "7", "showname": "显示属性联动" },
                                { "key": "8", "showname": "字段联动" },
                                { "key": "9", "showname": "公式/代码块绑定事件" }
                            ]}
                            value={linkage_typeValue}
                            onChange={this.linkTypeChange.bind(this)}
                        />
                    </WeaFormItem>
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@mn3oc2`}
                        style={{ width: '70%' }}
                        label={'查询字段'}
                        labelCol={{ span: 2 }}
                        wrapperCol={{ span: 12 }}
                    >
                        <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@akiuv1`}
                            style={{ width: '20%' }}
                            options={[
                                { "key": "0", "showname": "全部" },
                                { "key": "1", "showname": "触发字段" },
                                { "key": "2", "showname": "赋值字段" }
                            ]}
                            value={linkage_fieldTypeValue}
                            onChange={this.fieldTypeChange.bind(this)}
                        />
                        <WeaSelectTransfer ecId={`${this && this.props && this.props.ecId || ''}_WeaSelectTransfer@noln95`}
                            value={linkage_fieldValue}
                            options={fieldOptions}
                            onChange={this.fieldChange.bind(this)}
                        />
                    </WeaFormItem>
                </div>
                <div className='wf-linkageInfo-table' >
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@z9vb12`} columns={columns} dataSource={this.getDataSource()} pagination={false} />
                </div>
            </div>
        )
    }
    linkTypeChange = (value) => {
        const { linkTypeChange } = this.props.analyzeStore;
        linkTypeChange(value);
    }
    fieldTypeChange = (value) => {
        const { fieldTypeChange } = this.props.analyzeStore;
        fieldTypeChange(value);
    }
    fieldChange = (strArr) => {
        const { linkage_changeFieldValue } = this.props.analyzeStore;
        linkage_changeFieldValue(strArr);
    }
    getDataSource = () => {
        const WfForm = window.opener.WfForm;
        const layoutStore = WfForm.getLayoutStore();
        const changeFnObj = WfForm.__propertyChangeFnObj;
        const { tableInfo } = layoutStore;
        const { rowCalCfg, fieldSqlCfg, fieldMathCfg, dataInputCfg, fieldDateCfg, viewAttrCfg, mainCalCfg } = layoutStore.linkageCfg;
        const { fieldMap, showFieldID, mergeMark, linkage_typeValue, linkage_fieldTypeValue, linkage_fieldValue } = this.props.analyzeStore;

        let dataSource = [];
        rowCalCfg && _.forIn(rowCalCfg, (value, key) => {
            mergeMark ?
                key !== 'rowCalAssignInfo' &&
                value.map(item => {
                    dataSource.push(
                        {
                            linkageType: '行规则',
                            trgField: singleConvert(key, fieldMap, showFieldID),
                            assignField: singleConvert(item.assignField, fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                }) :
                key == 'rowCalAssignInfo' &&
                _.forIn(value, (v, k) => {
                    dataSource.push(
                        {
                            linkageType: '行规则',
                            trgField: multiConvert(v, fieldMap, showFieldID),
                            assignField: singleConvert(k, fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
        });
        mainCalCfg && _.forIn(mainCalCfg, (value, key) => {
            dataSource.push(
                {
                    linkageType: '列赋值',
                    trgField: singleConvert(key, fieldMap, showFieldID),
                    assignField: singleConvert(value, fieldMap, showFieldID),
                    otherInfo: '',
                }
            )
        });
        fieldSqlCfg && _.forIn(fieldSqlCfg, (value, key) => {
            if (key === 'emptyTriField') {
                value.map(item => {
                    dataSource.push(
                        {
                            linkageType: 'SQL联动',
                            trgField: '无',
                            assignField: singleConvert(item.assignField, fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            } else {
                value.map(item => {
                    dataSource.push(
                        {
                            linkageType: 'SQL联动',
                            trgField: singleConvert(key, fieldMap, showFieldID),
                            assignField: singleConvert(item.assignField, fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            }
        });
        let dateCfgArr = [];
        fieldDateCfg && _.forIn(fieldDateCfg, (value, key) => {
            if (mergeMark) {
                value.map(item => {
                    dataSource.push(
                        {
                            linkageType: '时间日期计算',
                            trgField: singleConvert(key, fieldMap, showFieldID),
                            assignField: singleConvert(item.assignField, fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            } else {
                value.map(item => {
                    dateCfgArr.push(
                        {
                            linkageType: '时间日期计算',
                            trgField: multiConvert(item.relateFields, fieldMap, showFieldID),
                            assignField: singleConvert(item.assignField, fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            }
        });
        //合并去次重
        dataSource = dataSource.concat(doDuplicate(dateCfgArr));

        fieldMathCfg && _.forIn(fieldMathCfg, (value, key) => {
            if (mergeMark) {
                value.map(item => {
                    dataSource.push(
                        {
                            linkageType: '字段赋值',
                            trgField: singleConvert(key, fieldMap, showFieldID),
                            assignField: singleConvert(item.assignField, fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            } else {
                let fieldArr = [];
                value.map(item => {
                    fieldArr.push(item.assignField);
                })
                dataSource.push(
                    {
                        linkageType: '字段赋值',
                        trgField: singleConvert(key, fieldMap, showFieldID),
                        assignField: multiConvert(fieldArr, fieldMap, showFieldID),
                        otherInfo: '',
                    }
                )
            }
        });
        dataInputCfg && _.forIn(dataInputCfg, (value, key) => {
            if (mergeMark) {
                value.map(item => {
                    item.otherParams.assignfields.map(field => {
                        dataSource.push(
                            {
                                linkageType: '字段联动',
                                trgField: singleConvert(key, fieldMap, showFieldID),
                                assignField: singleConvert(field, fieldMap, showFieldID),
                                otherInfo: '',
                            }
                        )
                    })
                })
            } else {
                let fieldArr = [];
                value.map(item => {
                    fieldArr = fieldArr.concat(item.otherParams.assignfields);
                })
                dataSource.push(
                    {
                        linkageType: '字段联动',
                        trgField: singleConvert(key, fieldMap, showFieldID),
                        assignField: multiConvert(fieldArr, fieldMap, showFieldID),
                        otherInfo: '',
                    }
                )
            }

        });
        viewAttrCfg && _.forIn(viewAttrCfg, (value, key) => {
            if (mergeMark) {
                value.relateFields && value.relateFields.map(item => {
                    let trgField = key.slice(0, -2);
                    let assignField = item.slice(0, -2);
                    dataSource.push(
                        {
                            linkageType: '显示属性联动',
                            trgField: singleConvert(trgField, fieldMap, showFieldID),
                            assignField: singleConvert(assignField, fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                })
            } else {
                let fieldArr = [];
                let trgField = key.slice(0, -2);
                value.relateFields && value.relateFields.map(item => {
                    fieldArr.push(item.slice(0, -2));
                })
                dataSource.push(
                    {
                        linkageType: '显示属性联动',
                        trgField: singleConvert(trgField, fieldMap, showFieldID),
                        assignField: multiConvert(fieldArr, fieldMap, showFieldID),
                        otherInfo: '',
                    }
                )
            }
        });
        tableInfo && _.forIn(tableInfo, formInfo => {
            formInfo.fieldinfomap && _.forIn(formInfo.fieldinfomap, (value, key) => {
                if (value.htmltype == 5 && value.selectattr.childfieldid > 0) {
                    dataSource.push(
                        {
                            linkageType: '选择框联动',
                            trgField: singleConvert(key, fieldMap, showFieldID),
                            assignField: singleConvert(value.selectattr.childfieldid.toString(), fieldMap, showFieldID),
                            otherInfo: '',
                        }
                    )
                }
            })
        })
        changeFnObj && _.forIn(changeFnObj, (v, key) => {
            dataSource.push(
                {
                    linkageType: '公式/代码块绑定事件',
                    trgField: singleConvert(key.substring(5), fieldMap, showFieldID),
                    assignField: '',
                    otherInfo: v.length + '条',
                }
            )
        })

        linkage_typeValue && (
            linkage_typeValue == '0' && null,
            linkage_typeValue == '1' && (dataSource = dataSource.filter(item => item.linkageType === '行规则')),
            linkage_typeValue == '2' && (dataSource = dataSource.filter(item => item.linkageType === '列赋值')),
            linkage_typeValue == '3' && (dataSource = dataSource.filter(item => item.linkageType === 'SQL联动')),
            linkage_typeValue == '4' && (dataSource = dataSource.filter(item => item.linkageType === '时间日期计算')),
            linkage_typeValue == '5' && (dataSource = dataSource.filter(item => item.linkageType === '字段赋值')),
            linkage_typeValue == '6' && (dataSource = dataSource.filter(item => item.linkageType === '选择框联动')),
            linkage_typeValue == '7' && (dataSource = dataSource.filter(item => item.linkageType === '显示属性联动')),
            linkage_typeValue == '8' && (dataSource = dataSource.filter(item => item.linkageType === '字段联动')),
            linkage_typeValue == '9' && (dataSource = dataSource.filter(item => item.linkageType === '公式/代码块绑定事件'))
        );
        let fieldList = !!linkage_fieldValue ? linkage_fieldValue.split(",") : [];
        let filterData = [];
        fieldList.length == 0 ? null :
            showFieldID ?   //id模式
                fieldList.map(fieldid => {
                    linkage_fieldTypeValue == 0 && (filterData = filterData.concat(dataSource.filter(item => {
                        let trgFieldArr = typeof item.trgField === "string" ? item.trgField.split(",") : item.trgField;
                        let assignFieldArr = typeof item.assignField === "string" ? item.assignField.split(",") : item.assignField;
                        let flag = false;
                        trgFieldArr.map(item => {
                            if ((typeof item !== "string" && item.props && item.props.children && item.props.children == fieldid) || (typeof item === "string" && item == fieldid)) flag = true;
                        })
                        assignFieldArr.map(item => {
                            if ((typeof item !== "string" && item.props && item.props.children && item.props.children == fieldid) || (typeof item === "string" && item == fieldid)) flag = true;
                        })
                        return flag;
                    })));
                    linkage_fieldTypeValue == 1 && (filterData = filterData.concat(dataSource.filter(item => {
                        let trgFieldArr = typeof item.trgField === "string" ? item.trgField.split(",") : item.trgField;
                        let flag = false;
                        trgFieldArr.map(item => {
                            if ((typeof item !== "string" && item.props && item.props.children && item.props.children == fieldid) || (typeof item === "string" && item == fieldid)) flag = true;
                        })
                        return flag;
                    })));
                    linkage_fieldTypeValue == 2 && (filterData = filterData.concat(dataSource.filter(item => {
                        let assignFieldArr = typeof item.assignField === "string" ? item.assignField.split(",") : item.assignField;
                        let flag = false;
                        assignFieldArr.map(item => {
                            if ((typeof item !== "string" && item.props && item.props.children && item.props.children == fieldid) || (typeof item === "string" && item == fieldid)) flag = true;
                        })
                        return flag;
                    })));
                })
                :         //字段模式
                fieldList.map(fieldid => {
                    let fieldName = singleConvert(fieldid, fieldMap, false);
                    linkage_fieldTypeValue == 0 && (filterData = filterData.concat(dataSource.filter(item => {
                        let trgFieldArr = typeof item.trgField === "string" ? item.trgField.split(",") : item.trgField;
                        let assignFieldArr = typeof item.assignField === "string" ? item.assignField.split(",") : item.assignField;
                        let flag = false;
                        trgFieldArr.map(item => {
                            if ((typeof item !== "string" && item.props && item.props.children && item.props.children == fieldName) || (typeof item === "string" && item == fieldName)) flag = true;
                        })
                        assignFieldArr.map(item => {
                            if ((typeof item !== "string" && item.props && item.props.children && item.props.children == fieldName) || (typeof item === "string" && item == fieldName)) flag = true;
                        })
                        return flag;
                    })));
                    linkage_fieldTypeValue == 1 && (filterData = filterData.concat(dataSource.filter(item => {
                        let trgFieldArr = typeof item.trgField === "string" ? item.trgField.split(",") : item.trgField;
                        let flag = false;
                        trgFieldArr.map(item => {
                            if ((typeof item !== "string" && item.props && item.props.children && item.props.children == fieldName) || (typeof item === "string" && item == fieldName)) flag = true;
                        })
                        return flag;
                    })));
                    linkage_fieldTypeValue == 2 && (filterData = filterData.concat(dataSource.filter(item => {
                        let assignFieldArr = typeof item.assignField === "string" ? item.assignField.split(",") : item.assignField;
                        let flag = false;
                        assignFieldArr.map(item => {
                            if ((typeof item !== "string" && item.props && item.props.children && item.props.children == fieldName) || (typeof item === "string" && item == fieldName)) flag = true;
                        })
                        return flag;
                    })));
                })
        return fieldList.length == 0 ? dataSource : Array.from(new Set(filterData));
    }
}